home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / SPMATE12.ZIP / SPELLWND.PA$ / spellwnd.pas
Pascal/Delphi Source File  |  1993-07-23  |  22KB  |  742 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal for Windows                        }
  5. {       Standard windows unit for ObjectWindows         }
  6. {                                                       }
  7. {       Copyright (c) 1991 Borland International        }
  8. {                                                       }
  9. {=======================================================}
  10. {       Modifications to allows Spell Checker           }
  11. {       SpelMate DLL to be accesses.                    }
  12. {       Copyright (c) 1993 Aciran Software systems      }
  13. {*******************************************************}
  14.  
  15. unit SpellWnds;
  16.  
  17. {$R SpellWNDS.RES}
  18.  
  19. interface
  20.  
  21. uses WinTypes, WinProcs, WinDos, Objects, OWindows, ODialogs,
  22.   OMemory, OStdDlgs, Strings,Win31;
  23.  
  24. const
  25.   cm_spell   = 101;
  26.   MaxWordLen = 20;
  27.  
  28.  
  29. {create function prototypes}
  30. type
  31.   TSpelmateInit = function :integer;
  32.   TSpellCheck = function (AWord:Pchar):BOOL;
  33.   TSuggestWord = function(AWord:PChar):PChar;
  34.   TDisplayAtTop = procedure;
  35.  
  36.  
  37. {create a new edit type so we can get a handle direct to its text buffer
  38.  for faster scanning}
  39. type
  40.    PNewEdit = ^TNewEdit;
  41.    TNewEdit = object(TEdit)
  42.    function GetHandle:word;
  43. end;
  44.  
  45.  
  46. type
  47.  
  48.   { TSearchRec }
  49.   TSearchRec = record
  50.     SearchText: array[0..80] of Char;
  51.     CaseSensitive: Bool;
  52.     ReplaceText: array[0..80] of Char;
  53.     ReplaceAll: Bool;
  54.     PromptOnReplace: Bool;
  55.     IsReplace: Boolean;
  56.   end;
  57.  
  58.   { TEditWindow  }
  59.   PEditWindow = ^TEditWindow;
  60.   TEditWindow = object(TWindow)
  61.     Editor: PNewEdit;
  62.     SearchRec: TSearchRec;
  63.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  64.     constructor Load(var S: TStream);
  65.     procedure Store(var S: TStream);
  66.     procedure WMSize(var Msg: TMessage);
  67.       virtual wm_First + wm_Size;
  68.     procedure WMSetFocus(var Msg: TMessage);
  69.       virtual wm_First + wm_SetFocus;
  70.     procedure CMEditFind(var Msg: TMessage);
  71.       virtual cm_First + cm_EditFind;
  72.     procedure CMEditFindNext(var Msg: TMessage);
  73.       virtual cm_First + cm_EditFindNext;
  74.     procedure CMEditReplace(var Msg: TMessage);
  75.       virtual cm_First + cm_EditReplace;
  76.     procedure CMSpell(var Msg: TMessage); {Spell check option added}
  77.       virtual cm_First + cm_Spell;
  78.   private
  79.     procedure DoSearch;
  80.   end;
  81.  
  82.   { TFileWindow }
  83.   PFileWindow = ^TFileWindow;
  84.   TFileWindow = object(TEditWindow)
  85.     FileName: PChar;
  86.     IsNewFile: Boolean;
  87.     constructor Init(AParent: PWindowsObject; ATitle, AFileName: PChar);
  88.     destructor Done; virtual;
  89.     constructor Load(var S: TStream);
  90.     procedure Store(var S: TStream);
  91.     function CanClear: Boolean; virtual;
  92.     function CanClose: Boolean; virtual;
  93.     procedure NewFile;
  94.     procedure Open;
  95.     procedure Read;
  96.     procedure SetFileName(AFileName: PChar);
  97.     procedure ReplaceWith(AFileName: PChar);
  98.     function Save: Boolean;
  99.     function SaveAs: Boolean;
  100.     procedure SetupWindow; virtual;
  101.     procedure Write;
  102.     procedure CMFileNew(var Msg: TMessage);
  103.       virtual cm_First + cm_FileNew;
  104.     procedure CMFileOpen(var Msg: TMessage);
  105.       virtual cm_First + cm_FileOpen;
  106.     procedure CMFileSave(var Msg: TMessage);
  107.       virtual cm_First + cm_FileSave;
  108.     procedure CMFileSaveAs(var Msg: TMessage);
  109.       virtual cm_First + cm_FileSaveAs;
  110.   end;
  111.  
  112. const
  113.   REditWindow: TStreamRec = (
  114.     ObjType: 80;
  115.     VmtLink: Ofs(TypeOf(TEditWindow)^);
  116.     Load:    @TEditWindow.Load;
  117.     Store:   @TEditWindow.Store);
  118.  
  119. const
  120.   RFileWindow: TStreamRec = (
  121.     ObjType: 81;
  122.     VmtLink: Ofs(TypeOf(TFileWindow)^);
  123.     Load:    @TFileWindow.Load;
  124.     Store:   @TFileWindow.Store);
  125.  
  126. procedure RegisterStdWnds;
  127.  
  128. implementation
  129.  
  130. {make instances of the functions from our prototypes}
  131. var
  132. SpelmateInit : TSpelmateInit;
  133. SpellCheck : TSpellCheck;
  134. SuggestWord : TSuggestWord;
  135. DisplayAtTop : TDisplayAtTop;
  136.  
  137. { TSearchDialog }
  138.  
  139. const
  140.   sd_Search          = MakeIntResource($7F10);
  141.   sd_Replace         = MakeIntResource($7F11);
  142.   sd_BCSearch        = MakeIntResource($7F12);
  143.   sd_BCReplace       = MakeIntResource($7F13);
  144.   id_SearchText      = 100;
  145.   id_CaseSensitive   = 101;
  146.   id_ReplaceText     = 102;
  147.   id_ReplaceAll      = 103;
  148.   id_PromptOnReplace = 104;
  149.  
  150. type
  151.   PSearchDialog = ^TSearchDialog;
  152.   TSearchDialog = object(TDialog)
  153.     constructor Init(AParent: PWindowsObject; Template: PChar;
  154.       var SearchRec: TSearchRec);
  155.   end;
  156.  
  157. constructor TSearchDialog.Init(AParent: PWindowsObject; Template: PChar;
  158.   var SearchRec: TSearchRec);
  159. var
  160.   C: PWindowsObject;
  161. begin
  162.   TDialog.Init(AParent, Template);
  163.   C := New(PEdit, InitResource(@Self, id_SearchText,
  164.     SizeOf(SearchRec.SearchText)));
  165.   C := New(PCheckBox, InitResource(@Self, id_CaseSensitive));
  166.   if (Template = sd_Replace) or (Template = sd_BCReplace) then
  167.   begin
  168.     C := New(PEdit, InitResource(@Self, id_ReplaceText,
  169.       SizeOf(SearchRec.ReplaceText)));
  170.     C := New(PCheckBox, InitResource(@Self, id_ReplaceAll));
  171.     C := New(PCheckBox, InitResource(@Self, id_PromptOnReplace));
  172.   end;
  173.   TransferBuffer := @SearchRec;
  174. end;
  175.  
  176. function TNewEdit.GetHandle;
  177. begin
  178.   GetHandle := SendMessage(HWindow,EM_GETHANDLE,0,LongInt(0));
  179. end;
  180.  
  181. { TEditWindow }
  182.  
  183. { Constructor for a TEditWindow.  Initializes its data fields using passed
  184.   parameters and default values.  Constructs its child edit control. }
  185. constructor TEditWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  186. begin
  187.   TWindow.Init(AParent, ATitle);
  188.   {NOTE: we use our new edit type here}
  189.   Editor := New(PNewEdit, Init(@Self, 200, nil, 0, 0, 0, 0, 0, True));
  190.   with Editor^.Attr do
  191.     Style := Style or es_NoHideSel;
  192.   FillChar(SearchRec, SizeOf(SearchRec), #0);
  193. end;
  194.  
  195. { Load a TEditWindow from the given stream }
  196. constructor TEditWindow.Load(var S: TStream);
  197. begin
  198.   TWindow.Load(S);
  199.   GetChildPtr(S, Editor);
  200. end;
  201.  
  202. { Store a TEditWindow to the given stream }
  203. procedure TEditWindow.Store(var S: TStream);
  204. begin
  205.   TWindow.Store(S);
  206.   PutChildPtr(S, Editor);
  207. end;
  208.  
  209. { Responds to an incoming wm_Size message by resizing the child edit
  210.   control according to the size of the TEditWindow's client area. }
  211. procedure TEditWindow.WMSize(var Msg: TMessage);
  212. begin
  213.   TWindow.WMSize(Msg);
  214.   SetWindowPos(Editor^.HWindow, 0, -1, -1, Msg.LParamLo+2, Msg.LParamHi+2,
  215.     swp_NoZOrder);
  216. end;
  217.  
  218. { Responds to an incoming wm_SetFocus message by setting the focus to the
  219.   child edit control. }
  220. procedure TEditWindow.WMSetFocus(var Msg: TMessage);
  221. begin
  222.   SetFocus(Editor^.HWindow);
  223. end;
  224.  
  225. procedure TEditWindow.DoSearch;
  226. var
  227.   S: array[0..80] of Char;
  228.   P: Pointer;
  229.   Rslt: Integer;
  230. begin
  231.   Rslt := 0;
  232.   with SearchRec do
  233.     repeat
  234.       Rslt := Editor^.Search(-1, SearchText, CaseSensitive);
  235.       if Rslt = -1 then
  236.       begin
  237.         if not IsReplace or not ReplaceAll then
  238.         begin
  239.           P := @SearchText;
  240.           WVSPrintF(S, '"%0.60s" not found.', P);
  241.           MessageBox(HWindow, S, 'Find error', mb_OK + mb_IconExclamation);
  242.         end;
  243.       end
  244.       else
  245.         if IsReplace then
  246.           if not PromptOnReplace then Editor^.Insert(ReplaceText)
  247.           else
  248.           begin
  249.             Rslt := MessageBox(HWindow, 'Replace this occurrence?',
  250.               'Search/Replace', mb_YesNoCancel + mb_IconQuestion);
  251.             if Rslt = id_Yes then Editor^.Insert(ReplaceText)
  252.             else if Rslt = id_Cancel then Exit;
  253.           end;
  254.     until (Rslt = -1) or not ReplaceAll or not IsReplace;
  255. end;
  256.  
  257. procedure TEditWindow.CMEditFind(var Msg: TMessage);
  258. var
  259.   Dialog: PChar;
  260. begin
  261.   if BWCCClassNames then
  262.     Dialog := sd_BCSearch
  263.   else
  264.     Dialog := sd_Search;
  265.   if Application^.ExecDialog(New(PSearchDialog, Init(@Self,
  266.     Dialog, SearchRec))) = id_OK then
  267.   begin
  268.     SearchRec.IsReplace := False;
  269.     DoSearch;
  270.   end;
  271. end;
  272.  
  273. procedure TEditWindow.CMEditFindNext(var Msg: TMessage);
  274. begin
  275.   DoSearch;
  276. end;
  277.  
  278. procedure TEditWindow.CMEditReplace(var Msg: TMessage);
  279. var
  280.   Dialog: PChar;
  281. begin
  282.   if BWCCClassName